﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Database;

namespace TrafficLightSim
{
    //function of this class is to display all the menus for the users to choose
    public class Menu
    {
        SpriteFont font;
        List<string> ids = new List<string>();
        Test t = new Test();
        #region Windows GUI region

        ComboBox hLane = new ComboBox();
        ComboBox vLane = new ComboBox();
        ComboBox hProb = new ComboBox();
        ComboBox vProb = new ComboBox();
        Button applyButton = new Button();
        Button pauseButton = new Button();
        Button loadStartButton = new Button();
        ComboBox twoWayButton = new ComboBox();
        ComboBox simRun = new ComboBox();
        Button loadButton = new Button();
        Button saveButton = new Button();
        Button delLogButton = new Button();

        ComboBox loadBox = new ComboBox();
        ComboBox deleteBox = new ComboBox();
        Button okButton = new Button();
        Button delAllButton = new Button();
        TextBox saveTxt = new TextBox();

        private bool isDelete = false;
        private bool isLoad = false;

        #endregion

        #region Property Region

        public string ModeContent
        {
            get { return twoWayButton.Text; }
        }

        public string HLaneContent
        {
            get { return hLane.Text; }
        }

        public string VLaneContent
        {
            get { return vLane.Text; }
        }

        public string HProbContent
        {
            get { return hProb.Text; }
        }

        public string VProbContent
        {
            get { return vProb.Text; }
        }

        public string SimRunContent
        {
            get { return simRun.Text; }
        }

        #endregion

        #region constructor region
        public Menu(SpriteFont font)
        {
            this.font = font;
        }
        #endregion

        public void Initialize()
        {
            #region Initialize GUI
            hLane.Text = "1";
            hLane.Width = 50;
            hLane.Location = new System.Drawing.Point(135, 583);
            hLane.FlatStyle = FlatStyle.Popup;
            string[] hLaneText = new string[] { "1", "2", "3" };
            hLane.Items.AddRange(hLaneText);
            Control.FromHandle(Game1.GameRef.Window.Handle).Controls.Add(hLane);

            vLane.Text = "1";
            vLane.Width = 50;
            vLane.Location = new System.Drawing.Point(135, 620);
            vLane.FlatStyle = FlatStyle.Popup;
            string[] vLaneText = new string[] { "1", "2", "3", "4" };
            vLane.Items.AddRange(vLaneText);
            Control.FromHandle(Game1.GameRef.Window.Handle).Controls.Add(vLane);

            simRun.Text = "1";
            simRun.Width = 50;
            simRun.Location = new System.Drawing.Point(680, 583);
            simRun.FlatStyle = FlatStyle.Popup;
            string[] simRunText = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
            simRun.Items.AddRange(simRunText);
            Control.FromHandle(Game1.GameRef.Window.Handle).Controls.Add(simRun);

            hProb.Text = "0.5";
            hProb.Width = 50;
            hProb.Location = new System.Drawing.Point(420, 583);
            hProb.FlatStyle = FlatStyle.Popup;
            string[] hProbText = new string[] { "0", "0.1", "0.2", "0.3", "0.4", "0.5", "0.6",
            "0.7", "0.8", "0.9", "1"};
            hProb.Items.AddRange(hProbText);
            Control.FromHandle(Game1.GameRef.Window.Handle).Controls.Add(hProb);

            vProb.Text = "0.5";
            vProb.Width = 50;
            vProb.Location = new System.Drawing.Point(420, 620);
            vProb.FlatStyle = FlatStyle.Popup;
            string[] vProbText = new string[] { "0", "0.1", "0.2", "0.3", "0.4", "0.5", "0.6",
            "0.7", "0.8", "0.9", "1"};
            vProb.Items.AddRange(vProbText);
            Control.FromHandle(Game1.GameRef.Window.Handle).Controls.Add(vProb);

            applyButton.Text = "Start";
            applyButton.Width = 70;
            applyButton.Location = new System.Drawing.Point(580, 615);
            applyButton.FlatStyle = FlatStyle.Popup;
            applyButton.Click += new System.EventHandler(StartButtonClick);
            Control.FromHandle(Game1.GameRef.Window.Handle).Controls.Add(applyButton);

            loadStartButton.Text = "Start With Load";
            loadStartButton.Width = 100;
            loadStartButton.Location = new System.Drawing.Point(660, 615);
            loadStartButton.FlatStyle = FlatStyle.Popup;
            loadStartButton.Click += new System.EventHandler(LoadStartClick);
            loadStartButton.Visible = false;
            Control.FromHandle(Game1.GameRef.Window.Handle).Controls.Add(loadStartButton);

            pauseButton.Text = "Pause";
            pauseButton.Width = 70;
            pauseButton.Location = new System.Drawing.Point(490, 615);
            pauseButton.FlatStyle = FlatStyle.Popup;
            pauseButton.Click += new System.EventHandler(PauseButtonClick);
            Control.FromHandle(Game1.GameRef.Window.Handle).Controls.Add(pauseButton);

            saveButton.Text = "Save";
            saveButton.Width = 70;
            saveButton.Location = new System.Drawing.Point(240, 660);
            saveButton.FlatStyle = FlatStyle.Popup;
            saveButton.Click += new System.EventHandler(SaveButtonClick);
            Control.FromHandle(Game1.GameRef.Window.Handle).Controls.Add(saveButton);

            loadButton.Text = "Load";
            loadButton.Width = 70;
            loadButton.Location = new System.Drawing.Point(330, 660);
            loadButton.FlatStyle = FlatStyle.Popup;
            loadButton.Click += new System.EventHandler(LoadButtonClick);
            Control.FromHandle(Game1.GameRef.Window.Handle).Controls.Add(loadButton);

            delLogButton.Text = "Delete Log";
            delLogButton.Width = 70;
            delLogButton.Location = new System.Drawing.Point(420, 660);
            delLogButton.FlatStyle = FlatStyle.Popup;
            delLogButton.Click += new System.EventHandler(DeleteButtonClick);
            Control.FromHandle(Game1.GameRef.Window.Handle).Controls.Add(delLogButton);

            twoWayButton.Text = "Normal";
            twoWayButton.Width = 70;
            twoWayButton.Location = new System.Drawing.Point(150, 660);
            twoWayButton.FlatStyle = FlatStyle.Popup;
            string[] twoWayText = new string[] { "Normal", "Two Way" };
            twoWayButton.Items.AddRange(twoWayText);
            Control.FromHandle(Game1.GameRef.Window.Handle).Controls.Add(twoWayButton);

            okButton.Text = "OK";
            okButton.Width = 50;
            okButton.Location = new System.Drawing.Point(600, 660);
            okButton.Click += new System.EventHandler(OKButtonClick);
            Control.FromHandle(Game1.GameRef.Window.Handle).Controls.Add(okButton);
            okButton.Visible = false;

            #endregion
        }

        private void StartButtonClick(object sender, EventArgs e)
        {
            if (!Game1.simOn)
            {
                Game1.simOn = true; //start sim
                Game1.simChange = 1;
            }
            else if (Game1.simOn)
            {
                Game1.simOn = false; //stop sim
                Game1.simChange = 0;
                Game1.GameRef.Time = 0;
            }

        }

        private void SaveButtonClick(object sender, EventArgs e)
        {
            if (!Game1.simOn)
            {
                Game1.GameRef.Sim.SaveDataExec();
                t.Save(Game1.GameRef.Sim.SaveDat, Game1.GameRef.Sim.CDat);
                if (t.SaveSuccess)
                {
                    MessageBox.Show("Saved!");
                }
            }

        }

        private void LoadButtonClick(object sender, EventArgs e)
        {
            if (!Game1.simOn)
            {
                ids.Clear(); //clear the list
                ids = t.DisplayLoad();
                loadBox.Items.Clear();
                loadBox.Width = 70;
                loadBox.Location = new System.Drawing.Point(520, 660);
                loadBox.FlatStyle = FlatStyle.Popup;
                string[] idArr = ids.ToArray();
                loadBox.Items.AddRange(idArr);
                loadBox.Visible = true;
                Control.FromHandle(Game1.GameRef.Window.Handle).Controls.Add(loadBox);

                isLoad = true;

                okButton.Visible = true;
            }
        }

        private void LoadStartClick(object sender, EventArgs e)
        {
            Game1.GameRef.Sim.LoadSim(t.CarData, t.SaveData.Last());
            Game1.simOn = true;
            loadStartButton.Visible = false;
            //load start code

        }

        private void OKButtonClick(object sender, EventArgs e)
        {
            if (isLoad)
            {
                loadBox.Visible = false;
                t.Load(loadBox.Text);
                loadStartButton.Visible = true;
                isLoad = false;
            }

            else if (isDelete)
            {
                deleteBox.Visible = false;
                delAllButton.Visible = false;
                loadBox.Visible = false;
                loadStartButton.Visible = false;
                t.Delete(deleteBox.Text);
                isDelete = false;
            }
            okButton.Visible = false;
        }

        private void DeleteButtonClick(object sender, EventArgs e)
        {
            if (!Game1.simOn)
            {
                ids.Clear(); //clear the list
                ids = t.DisplayLoad();
                deleteBox.Items.Clear();
                deleteBox.Width = 70;
                deleteBox.Location = new System.Drawing.Point(520, 660);
                deleteBox.FlatStyle = FlatStyle.Popup;
                string[] idArr = ids.ToArray();
                deleteBox.Items.AddRange(idArr);
                deleteBox.Visible = true;
                Control.FromHandle(Game1.GameRef.Window.Handle).Controls.Add(deleteBox);

                delAllButton.Width = 100;
                delAllButton.Text = "Delete All";
                delAllButton.Location = new System.Drawing.Point(660, 660);
                loadStartButton.Click += new System.EventHandler(DeleteAllClick);
                Control.FromHandle(Game1.GameRef.Window.Handle).Controls.Add(delAllButton);
                delAllButton.Visible = true;

                isDelete = true;

                okButton.Visible = true;
            }
        }

        private void DeleteAllClick(object sender, EventArgs e)
        {
            delAllButton.Visible = false;
            deleteBox.Visible = false;
            okButton.Visible = false;
            t.DeleteAll();
            
        }

        private void TwoWayButtonClick(object sender, EventArgs e)
        {
            Game1.GameRef.ToggleTwoWay();
        }

        private void PauseButtonClick(object sender, EventArgs e)
        {
            if (!Game1.simOn && pauseButton.Text == "Resume")
            {
                Game1.simOn = true;
                pauseButton.Text = "Pause";
            }
            else if (Game1.simOn)
            {
                Game1.simOn = false;
                pauseButton.Text = "Resume";
            }

        }

        //change the apply button text
        public void Update()
        {
            if (Game1.simOn)
                applyButton.Text = "Stop";
            else
            {
                applyButton.Text = "Start";
                //pauseButton.Text = "Pause";
            }
        }

        public void Draw()
        {
            Game1.GameRef.spriteBatch.DrawString(font,
                "Horizontal lane: ",
                new Vector2(10, 580),
                Color.Black);

            Game1.GameRef.spriteBatch.DrawString(font,
                "Vertical lane: ",
                new Vector2(20, 620),
                Color.Black);

            Game1.GameRef.spriteBatch.DrawString(font,
                "Simulation Mode: ",
                new Vector2(10, 660),
                Color.Black);

            Game1.GameRef.spriteBatch.DrawString(font,
                "Horizontal Lane Probability: ",
                new Vector2(200, 580),
                Color.Black);

            Game1.GameRef.spriteBatch.DrawString(font,
                "Vertical Lane Probability: ",
                new Vector2(200, 620),
                Color.Black);

            Game1.GameRef.spriteBatch.DrawString(font,
                "Number of Simulation(s): ",
                new Vector2(480, 580),
                Color.Black);
        }
    }
}
